﻿using System;
using System.Collections.Generic;
using System.Text;
using XDesigner.AppFramework;
using XDesigner.AppFramework.WinForms;
using System.Collections;
using 刘颜红销售项目管理.Business.DOM;
using System.Drawing;

namespace 刘颜红销售项目管理.Business
{
    public class StructureTreeProvider : DomTreeProvider
    {
        public StructureTreeProvider()
        {
        }

        private bool _ShowActionNode = false;
        /// <summary>
        /// 是否显示功能性的标示节点
        /// </summary>
        [System.ComponentModel.DefaultValue(false)]
        public bool ShowActionNode
        {
            get
            {
                return _ShowActionNode;
            }
            set
            {
                _ShowActionNode = value;
            }
        }


        public static object _项目基本信息 = new object();
        public static object _产品型号 = new object();
        public static object _基础代码维护 = new object();
        public static object _工作日志 = new object();

        /// <summary>
        /// 获得各种节点的子节点对象
        /// </summary>
        /// <param name="parentInstance"></param>
        /// <returns></returns>
        public override System.Collections.IList OnGetChildInstances(object parentInstance)
        {
            if ( parentInstance is MyApplicationFramework)
            {
                ArrayList list = new ArrayList();
                if (this.ShowActionNode)
                {
                    list.Add(_项目基本信息);
                    list.Add(_产品型号);
                    list.Add(_基础代码维护);
                    list.Add(_工作日志);
                }
                list.AddRange(((MyApplicationFramework)parentInstance).单位列表);
                list.Add( Class未分类工作单位.Instance);
                return list;
            }
            else if (parentInstance is Class工作单位)
            {
                return ((Class工作单位)parentInstance).人员列表;
            }
            else if (parentInstance == _项目基本信息)
            {
                return MyApplicationFramework.Instance.项目列表;
            }
            return null;
        }

        /// <summary>
        /// 获得各种节点的父节点对象
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public override object OnGetParentInstance(object instance)
        {
            if ( instance is Class工作单位)
            {
                return MyApplicationFramework.Instance;
            }
            else if (instance is Class人员基本信息)
            {
                return ((Class人员基本信息)instance).所属单位;
            }
            return null;
        }

        /// <summary>
        /// 获得各种节点的状态
        /// </summary>
        /// <param name="parentInstance"></param>
        /// <param name="instance"></param>
        /// <returns></returns>
        public override DomTreeEventArgs OnGetNodeStates(object parentInstance, object instance)
        {
            DomTreeEventArgs args = new DomTreeEventArgs();
            if (instance == _产品型号)
            {
                args.Text = "产品型号";
                args.Image = ImageContainer.Instance[_产品型号];
                if (args.Image == null)
                {
                    ImageContainer.Instance[_产品型号] = ToolboxBitmapAttribute.GetImageFromResource(this.GetType(), "Images.Flag.bmp", false);
                    args.Image = ImageContainer.Instance[_产品型号];
                }
                args.HasChildInstances = false;
                return args ;
            }
            else if (instance == _基础代码维护)
            {
                args.Text = "基础代码";
                args.Image = ImageContainer.Instance[_产品型号];
                if (args.Image == null)
                {
                    ImageContainer.Instance[_产品型号] = ToolboxBitmapAttribute.GetImageFromResource(this.GetType(), "Images.Flag.bmp", false);
                    args.Image = ImageContainer.Instance[_产品型号];
                }
                args.HasChildInstances = false;
                return args ;
            }
            else if (instance == _项目基本信息)
            {
                args.Text = "项目信息";
                args.Image = ImageContainer.Instance[_产品型号];
                if (args.Image == null)
                {
                    ImageContainer.Instance[_产品型号] = ToolboxBitmapAttribute.GetImageFromResource(this.GetType(), "Images.Flag.bmp", false);
                    args.Image = ImageContainer.Instance[_产品型号];
                }
                args.HasChildInstances = true ;
                return args ;
            }
            else if (instance == _工作日志)
            {
                args.Text = "工作日志";
                args.Image = ImageContainer.Instance[_工作日志];
                if (args.Image == null)
                {
                    ImageContainer.Instance[_工作日志] = ToolboxBitmapAttribute.GetImageFromResource(this.GetType(), "Images.Flag.bmp", false);
                    args.Image = ImageContainer.Instance[_工作日志];
                }
                args.HasChildInstances = false;
                return args;
            }
            else if (instance is MyApplicationFramework)
            {
                MyApplicationFramework work = (MyApplicationFramework)instance;
                args.Text = work.Title;
                args.HasChildInstances = true;
            }
            else if (instance is Class工作单位)
            {
                Class工作单位 unit = (Class工作单位)instance;
                args.Text = unit.单位名称;
                args.HasChildInstances = true;
            }
            else if (instance is Class人员基本信息)
            {
                Class人员基本信息 people = (Class人员基本信息)instance;
                args.Text = people.姓名;
                args.HasChildInstances = false;
            }
            else if (instance is Class项目基本信息)
            {
                Class项目基本信息 project = (Class项目基本信息)instance;
                args.Text = project.项目名称;
                args.HasChildInstances = false;
            }
            Type type = instance.GetType();
            args.Image = ImageContainer.Instance[type];
            if (args.Image == null)
            {
                System.Drawing.ToolboxBitmapAttribute attr = (ToolboxBitmapAttribute)Attribute.GetCustomAttribute(type, typeof(ToolboxBitmapAttribute), true);
                if (attr != null)
                {
                    args.Image = ImageContainer.Instance[instance.GetType()];
                    if (args.Image == null)
                    {
                        args.Image = attr.GetImage(instance);
                        ImageContainer.Instance[type] = args.Image;
                    }
                }
            }//if
            return args;
        }
    }
}